package com.konne.nightmare.FastPublicOpinion.utils;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.konne.nightmare.FastPublicOpinion.base.MyApplication;


public class GaoDeMapUtils implements AMapLocationListener {
    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;
    private AMapLocationClientOption locationOption = null;//定位设置
    private OnLocationPass onLocationPass;

    public GaoDeMapUtils(Context context) {
        //初始化定位
        if (null==mLocationClient) {
            try {
                mLocationClient = new AMapLocationClient(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null==locationOption)
        locationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationClient.setLocationListener(this);
        locationOption.setOnceLocation(true);//单次定位
        locationOption.setNeedAddress(true);//逆地理编码
        mLocationClient.setLocationOption(locationOption);//设置定位参数
        mLocationClient.startLocation();//启动定位

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        //具体地址
        String address = aMapLocation.getAddress();
//        //城市
//        String city = aMapLocation.getCity();
//        //经纬度
//        double latitude = aMapLocation.getLatitude();
//        double longitude = aMapLocation.getLongitude();
        Log.e("duanlian", "onLocationChanged: " + address);
//        if (!"".equals(city)) {
            //通过接口回调把定位的城市传递过去
        MyApplication.aMapLocation=aMapLocation;

        onLocationPass.onLocation(aMapLocation);
//        }
    }

    public void setOnLocationPass(OnLocationPass locationPass) {
        this.onLocationPass = locationPass;
    }

    public interface OnLocationPass {
        void onLocation(AMapLocation aMapLocation);

    }
}